package com.bjtu.literatureclient.service.user.impl;

import com.bjtu.literatureclient.dao.user.UserIndexDao;
import com.bjtu.literatureclient.domain.user.UserIndexNoTable;
import com.bjtu.literatureclient.service.user.UserIndexService;
import com.bjtu.literatureclient.util.ChineseToPinyin;
import com.bjtu.literatureclient.util.StringToChar;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Service("userIndexNoService")
public class UserIndexServiceImpl implements UserIndexService {

	@Autowired
	private UserIndexDao userIndexDao;
	@Override
	public String createIndexNo(int language, String firstName, String lastName ,String year, String startCode) {
        if (language == 2) {
        	try {
				lastName = ChineseToPinyin.convertChineseToPinyin(lastName, false,HanyuPinyinCaseType.LOWERCASE);
			} catch (BadHanyuPinyinOutputFormatCombination e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	
            lastName += StringToChar.stringToChar(firstName);
        } else {
        	try {
				lastName = ChineseToPinyin.convertChineseToPinyin(lastName, false,HanyuPinyinCaseType.LOWERCASE);
			} catch (BadHanyuPinyinOutputFormatCombination e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }

        String indexNo=lastName + year;
        System.out.println(indexNo);
        char code = startCode.charAt(0);
		int size=0;
        if (language == 2) {
        	size = userIndexDao.getDistinctIndex(indexNo);
        }else{
        	size = userIndexDao.getDistinctIndex1(indexNo);
        }
 
		code+=size;
        return indexNo + code;
    
	}

	private String toGBK(String lastName) {
		byte[] bytes;
		try {
			bytes = lastName.getBytes("utf-8");
			byte[] bytes2 = new String(bytes, "utf-8").getBytes("gbk");  
			  
			lastName=new String(bytes2, "gbk"); 
			return lastName;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;  
	}

	private String toUTF(String lastName) {
		byte[] bytes;
		try {
			bytes = lastName.getBytes("gbk");
			byte[] bytes2 = new String(bytes, "gbk").getBytes("utf-8");  
			  
			lastName=new String(bytes2, "utf-8"); 
			return lastName;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;  
	}
	public static void main(String[] args) {
		
	}

	@Override
	public List<UserIndexNoTable> queryAllIndexByUser(String userNo) {
		// TODO Auto-generated method stub
		return userIndexDao.queryAllIndexByUser(userNo);
	}

}
